package co.fitstart.admin.web.controller.trade.order;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.search.AllOrderSearch;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.OrderService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.principal.Principal;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("orders-search")
@MenuSetting("menu-ecommerce")
public class OrderSearchController {
        
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private PrincipalService principalService;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-order-search")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("ecommerce/order/order-search-list");
        
        return modelAndView;
    }
    
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-order-search")
    public ModelAndView onSearch(@Secure Login login,
                                 AllOrderSearch allOrderSearch,
                                 @RequestParam(value = "siteId", required = false) Long siteId,
                                 @RequestParam(value = "orderType", required = false) String orderType,
                                 HttpServletRequest request,
                                 Page page) {
        ModelAndView modelAndView = new ModelAndView("ecommerce/order/order-search-list");
        
        if(login.hasSite()) {
            allOrderSearch.setSite(login.getSite());
        } else if(siteId != null) {
            allOrderSearch.setSite(siteService.load(siteId));
        }
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            allOrderSearch.setSites(siteService.listSite(principal));
        }
        
        if(allOrderSearch.getSites() != null && allOrderSearch.getSites().isEmpty()) {
            modelAndView.addObject("total",0);
        } else {
            OrderType type = OrderType.aliasOf(orderType);
            List<OrderType> orderTypes = new ArrayList<OrderType>();
            
            if(login.isSiteManager() || login.isSiteServer() || login.isAreaManager() || login.isAreaServer()) {
                //不能看线上订单、周边订单
                if(type == OrderType.INDIVIDUAL || type == OrderType.GROUP || type == OrderType.YEAR) {
                    orderTypes.add(type);
                } else {
                    orderTypes.add(OrderType.INDIVIDUAL);
                    orderTypes.add(OrderType.GROUP);
                    orderTypes.add(OrderType.YEAR);
                }
                
                allOrderSearch.setOrderTypes(orderTypes);
            } else if(login.isCustomerServer() || login.isOnlineCoach()) {
                //只能看线上订单
                orderTypes.add(OrderType.ONLINE);

                allOrderSearch.setOrderTypes(orderTypes);
            } else if(login.isCreditProductManager()) {
                //只能看周边订单
                orderTypes.add(OrderType.OTHERS);

                allOrderSearch.setOrderTypes(orderTypes);
            } else {
                if(type != null) {
                    orderTypes.add(type);
                    
                    allOrderSearch.setOrderTypes(orderTypes);
                }
            }
            
            //线上、周边订单无门店
            if(type == OrderType.ONLINE || type == OrderType.OTHERS) {
                allOrderSearch.setSite(null);
            }
            
            // list order by page & status
            List<Order> orders = orderService.search(allOrderSearch, page);
            modelAndView.addObject("orders",orders);
            
            // get total
            int total = orderService.totalOrder(allOrderSearch);
            modelAndView.addObject("total",total);
        }
        
        // add query string
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
    }
}
